/**
 * 初始化整体流量图标
 */
define(function(){
    var ec=require('echarts');
    require('echarts/chart/line');
    require('echarts/chart/bar');

    var myChart = ec.init(document.getElementById('platPostRate_EC'));

    var placeHoledStyle = {
        normal:{
            barBorderColor:'rgba(0,0,0,0)',
            color:'rgba(0,0,0,0)'
        },
        emphasis:{
            barBorderColor:'rgba(0,0,0,0)',
            color:'rgba(0,0,0,0)'
        }
    };
    var dataStyle = { 
        normal: {
            label : {
                show: true,
                position: 'insideLeft',
                formatter: '{c}%'
            }
        }
    };

    var option_day = {
        tooltip : {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            },
            formatter : '{b}<br/>{a0}:{c0}%<br/>{a2}:{c2}%<br/>{a4}:{c4}%'
        },
        legend: {
            y: 15,
            // itemGap : document.getElementById('main').offsetWidth / 8,
            data:['PC', 'M','APP']
        },
        toolbox: {
            show : true,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        grid: {
            y: 40,
            y2: 30
        },
        xAxis : [
            {
                type : 'value',
                position: 'top',
                splitLine: {show: false},
                axisLabel: {show: false}
            }
        ],
        yAxis : [
            {
                type : 'category',
                splitLine: {show: false},
                data : ['招聘', '房产', '黄页', '二手','二手车']
            }
        ],
        series : [
            {
                name:'GML',
                type:'bar',
                stack: '总量',
                itemStyle : dataStyle,
                data:[38, 50, 33, 72]
            }
        ]
    };



    /**
     * 根据指标显示日数据
     * @param eventType= 2:第一次加载, 1:切换日，周，月刷新 , 0: 添加对比后又重新选择指标
     */
    function show(platSrcLine,eventType){
        var indexType = $('#total_index').val();
        var dateGrp = $('#platPostRateOption').attr('dateGrp');
        var beginDate = $('#beginDate_platPostRate').val();
        var endDate = $('#endDate_platPostRate').val();

        myChart.showLoading({
            text: '正在努力的读取数据中...'
        });
        $.ajax({
            url: '/dashboard/info/ajax/platPostRate',
            type: 'post',
            async: true,
            data:{"dateGroup":dateGrp,"indexType":indexType, "beginDate":beginDate, "endDate":endDate},
            dataType: 'json',
            timeout:5000,
            success: function(data, textStatus) {
                var series_local = [];
                var xAxisList = ["PC","M","APP"];
                for(var i=0;i<xAxisList.length ;i++){
                    var curData =  data[xAxisList[i]];
                    series_local[2*i] = {
                        name: xAxisList[i],
                        type:'bar',
                        stack: '总量',
                        itemStyle : dataStyle,
                        data: curData
                    }
                    var hideData = $.map(curData,function(n){
                        return 100 - n;
                    })                    
                    series_local[2*i+1] = {
                        name: xAxisList[i],
                        type:'bar',
                        stack: '总量',
                        itemStyle : placeHoledStyle,
                        data: hideData
                    }
                }
                option_day.series = series_local;
                myChart.setOption(option_day,true);
                myChart.hideLoading();
            },
            error : function() {
                myChart.hideLoading();
            }
        });
    }


   //提示框
    function  tooltipFunc(params, ticket, callback){
        var dateVal = formatShowDate(params[0].name);
        var index_current=getIndexByName(option_day.xAxis[0].data,params[0].name);

        var result='<table class="table-center">';
        result+='<tr><td>'+dateVal+'</td><td>';
        var isStatDay = isDay(); 
        if(isStatDay)
            result += getWeekDay(dateVal);
        result += '</td>';
        if(isStatDay)
            result +="<td>日环比</td><td>周同比</td>";
        else if(isWeek())
            result += "<td>周环比</td>";
         else if(isMonth())
            result += "<td>月环比</td>";
        result += '</tr>';
        var total=0;


        $(params).each(function(index,para){
            result+='<tr><td>'+para.seriesName+'</td><td>'+zrnumber.addCommas(para.data)+'</td>';
           
            var seriesIndex = para.seriesIndex;
            var day_td="<td>--</td>";
            //环比
            if(index_current-1>=0){
                var data_yesday = null;
                //去年同月
                // if(isMonth()){
                //     if(index_current-12>=0){
                //         data_yesday= option_day.series[seriesIndex].data[index_current-12];
                //     }
                // }else{ 
                    data_yesday= option_day.series[seriesIndex].data[index_current-1];                
                // }
                if(data_yesday != null){
                    var s=(((para.data/data_yesday)-1)*100).toFixed(2);
                    if(s>0){
                        day_td='<td style="color: orangered;">'+s+'%&#8593</td>';
                    }else{
                        day_td='<td style="color: greenyellow;">'+s+'%&#8595</td>';
                    }
                }
            }
            result += day_td;
            //周同比
            if(isStatDay){
                var week_td='<td>--</td>';
                if(index_current-7>=0){
                    data_week= option_day.series[seriesIndex].data[index_current-7];
                    var s=(((para.data/data_week)-1)*100).toFixed(2);
                    if(s>0){
                        week_td='<td style="color: orangered;">'+s+'%&#8593</td>';
                    }else{
                        week_td='<td style="color: greenyellow;">'+s+'%&#8595</td>';
                    }
                }
                result += week_td;
            }
            result += '</tr>';
        });
        result+='</table>';
        return result;
    }   

    //时间选择事件
    $('#beginDate_platPostRate').datepicker(datepicker_op);
    $('#endDate_platPostRate').datepicker(datepicker_op);
    
    $('#beginDate_platPostRate').datepicker().on('changeDate', function (e) {
        $('#endDate_platPostRate').datepicker('setStartDate', e.date);
    })

    $('#endDate_platPostRate').datepicker().on ('changeDate', function (e) {
        $('#beginDate_platPostRate').datepicker('setEndDate', e.date);
    });       

    //绑定刷新
    $("#refresh_platPostRate_btn").click(function(){
        EC.show(businessName,2);
    });

    var EC={
        show:show,
        resize:function(){myChart.resize();},

    };

    return EC;
});

